/**
 * @project spring-threadpool-async
 * @description
 * @errorcode SystemException</ br>
 * @author 2022 18:08 huzhengyang Create 1.0 <br>
 * @copyright ©2022-2023
 */

package com.teradata.async.core;

import org.springframework.stereotype.Component;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.ief.v1.IefAsyncClient;
import com.huaweicloud.sdk.ief.v1.IefClient;
import java.util.function.Function;

@Component
public class IefCaller {


    public IefClient iefSyncClient() {
        String ak = "my_ak";
        String sk = "my_sk";
        String iefToken = "12345";
        String endpoint = "http://10.202.40.100:30666/mock/129";
        String projectId = "49095ea8cc3148278e41ed4e7278b811";

        // Http Configuration for client
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // Create the authentication
        BasicCredentials auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);

        if (iefToken != null) {
            auth.setSecurityToken(iefToken);
        }

        // Create VpcClient instance
        return IefClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
    }


    public IefAsyncClient iefAsyncClient() {
        String ak = "my_ak";
        String sk = "my_sk";
        String token = "12345";
        String endpoint = "http://10.202.40.100:30666/mock/129";
        String projectId = "49095ea8cc3148278e41ed4e7278b811";

        /*if (authentication != null) {
            JsonNode additionalInfo = ((SecurityUser) authentication.getPrincipal()).getAdditionalInfo();
            SsoUser ssoUser = ssoCache.get(additionalInfo != null && additionalInfo.isObject() ?
                    // 此处为约定好的名称 sso_username
                    additionalInfo.get("sso_username").asText() :
                    ((SecurityUser) authentication.getPrincipal()).getEmail(), SsoUser.class);

            if (ssoUser != null && ssoUser.getExtended() != null) {
                ObjectNode extended = ssoUser.getExtended();
                token = extended.has("token") ? extended.get("token").asText() : "";
                endpoint = extended.has("endpoint") ? extended.get("endpoint").asText() : "";
                projectId = extended.has("projectId") ? extended.get("projectId").asText() : "";

            }
        }*/
        // Http Configuration for client
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // Create the authentication
        BasicCredentials auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withSecurityToken(token)
                .withProjectId(projectId);

        // Create VpcClient instance
        return IefAsyncClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
    }

    public <T> T iefSyncCall(Function<IefClient, T> function) {
        IefClient iefClient = iefSyncClient();
        if (iefClient != null) {
            return function.apply(iefClient);
        } else {
            return null;
        }
    }

    public <T> T iefAsyncCall(Function<IefAsyncClient, T> function) {
        IefAsyncClient iefAsyncClient = iefAsyncClient();
        if (iefAsyncClient != null) {
            return function.apply(iefAsyncClient);
        } else {
            return null;
        }
    }
}
